package com.bdqn.business.access.controller;

import com.bdqn.aop.Log;
import com.bdqn.business.access.service.UserService;
import com.bdqn.entity.access.Teacher;
import com.bdqn.entity.access.User;
import com.bdqn.entity.access.UserTeacher;
import com.bdqn.error.CommonException;
import com.bdqn.response.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Log("用户列表")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Result list(@RequestBody Map<String,Object> map){
        return userService.list(map);
    }


    @Log("添加用户")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result save(@RequestBody UserTeacher vo) throws Exception {

        User user = new User();
        Teacher teacher = new Teacher();

        BeanUtils.copyProperties(vo,user);
        BeanUtils.copyProperties(vo,teacher);

        return userService.save(user,teacher);
    }

    @Log("修改用户")
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Result update(@RequestBody UserTeacher vo){
        User user = new User();
        Teacher teacher = new Teacher();

        BeanUtils.copyProperties(vo,user);
        BeanUtils.copyProperties(vo,teacher);

        return userService.update(user,teacher);
    }


    //查询班级列表中显示的教员,助教,班主任的数据集合
    @Log("显示教员,助教,班主任的数据集合")
    @RequestMapping(value = "/findTeachersByJobTitle",method = RequestMethod.GET)
    public Result findTeachersByJobTitle() throws CommonException {
        return userService.findTeachersByJobTitle();
    }

}
